                .586
                TITLE   FILTER_NOMMX 
                .MODEL  FLAT, STDCALL



INTEL_P_Filter__NO_MMX	PROTO	C, Buf:DWORD, W:DWORD, H:DWORD, CenterScale:DWORD

INTEL_Extrem_Filter_MinMax__NO_MMX	PROTO	C, Buf:DWORD, W:DWORD, H:DWORD

INTEL_Extrem_Filter_Aver__NO_MMX	PROTO	C, Buf:DWORD, W:DWORD, H:DWORD, Max_Min:DWORD



;====================================================================
;				                                                    
;				M A C R O																
;																	
;====================================================================
RepeatADD	MACRO	Dest, Sorc, CenterRep

		Shift = 0
IRPC	Msk, <1248>
IF		(CenterRep and Msk)
IF		(Shift)
		SHL		Sorc,Shift
		Shift = 0
ENDIF
		ADD		Dest,Sorc
ENDIF
		Shift = Shift+1
ENDM
ENDM						


P_Filter8	MACRO	Dest, Top, Center, Bottom, CenterRep


IRPC	N,<01234567>
		MOVZX	ECX,BYTE PTR [Center-1+N]
		MOVZX	EBP,BYTE PTR [Center+N]
		
		;ADD		ECX,EBP	
		RepeatADD	ECX,EBP, CenterRep
						
		MOVZX	EBP,BYTE PTR [Center+1+N]
		ADD		ECX,EBP					
		MOVZX	EBP,BYTE PTR [Top+N]
		ADD		ECX,EBP					
		MOVZX	EBP,BYTE PTR [Bottom+N]
		ADD		ECX,EBP					
		IMUL	ECX,ECX,10000h/(4+CenterRep)
		SHR		ECX,16
		MOV		[Dest+N],CL
ENDM


ENDM


CopyLine	MACRO	Dest
LOCAL	L				
		LEA		ECX,[EBX+7]
		SHR		ECX,3
		MOV		EAX,0

		PUSH	EBX
L:		MOV		EBX,[EDX+EAX]
		MOV		[Dest+EAX],EBX
		MOV		EBX,[EDX+EAX+4]
		MOV		[Dest+EAX+4],EBX
		ADD		EAX,8
		LOOP	L
		POP		EBX

		MOV		AL,[Dest]
		MOV		[Dest-1],AL

		MOV		AL,[Dest+EBX-1]
		MOV		[Dest+EBX],AL
ENDM


CopyLine1	MACRO	Dest
LOCAL	L				
		LEA		ECX,[EBX+7]
		SHR		ECX,3
		MOV		EAX,0

		PUSH	EBX
L:		MOV		EBX,[EDX+EAX]
		MOV		[Dest+EAX],EBX
		MOV		EBX,[EDX+EAX+4]
		MOV		[Dest+EAX+4],EBX
		ADD		EAX,8
		LOOP	L
		POP		EBX

ENDM

MinMax4	MACRO	JABE	
LOCAL	B1, B2, B3, B4

		CMP		AL,CL
		JABE	B1
		MOV		AL,CL
B1:				
		CMP		AH,CH
		JABE	B2
		MOV		AH,CH
B2:		
		ROR		EAX,16
		ROR		ECX,16

		CMP		AL,CL
		JABE	B3
		MOV		AL,CL
B3:				
		CMP		AH,CH
		JABE	B4
		MOV		AH,CH
B4:		
		ROR		EAX,16
		ROR		ECX,16
ENDM


MinMax2	MACRO	JABE	
LOCAL	B1, B2

		CMP		AL,CL
		JABE	B1
		MOV		AL,CL
B1:				
		CMP		AH,CH
		JABE	B2
		MOV		AH,CH
B2:		
ENDM


MinMax	MACRO	JABE, dst, src	
LOCAL	B

		CMP		dst,src
		JABE	B
		MOV		dst,src
B:				
ENDM

Extrem_Filter8_MinMax	MACRO	Dest, Top, Center, Bottom

IRPC	N,<04>
		MOV		EAX,[Top+N]
		MOV		ECX,[Bottom+N]
		MinMax4	JBE
		MOV		[Min8+N],EAX

		MOV		EAX,[Top+N]
		MinMax4	JAE
		MOV		[Max8+N],EAX

		MOV		EAX,[Center+N]
		MOV		ECX,[Center+N+2]
		MinMax4	JBE
		MOV		[Min8Center+N],EAX

		MOV		EAX,[Center+N]
		MinMax4	JAE
		MOV		[Max8Center+N],EAX

ENDM

		MOV		AX,[Top+8]
		MOV		CX,[Bottom+8]
		MinMax2	JBE
		MOV		[Min8+8],AX

		MOV		AX,[Top+8]
		MinMax2	JAE
		MOV		[Max8+8],AX

		


IRPC	N,<01234567>
LOCAL	L1, L2, L3

		MOV		EAX,[Min8+N-1]
		MOV		ECX,[Max8+N-1]
		MOV		AL,[Min8Center+N]
		MOV		CL,[Max8Center+N]

		MinMax	JBE, AH,AL
		MinMax	JAE, CH,CL
		SHR		EAX,8
		SHR		ECX,8
		MinMax	JBE, AH,AL
		MinMax	JAE, CH,CL
		SHR		EAX,8
		SHR		ECX,8
		MinMax	JBE, AH,AL
		MinMax	JAE, CH,CL

		MOV		AL,[Center+N+1]
		CMP		AL,AH
		JA		L1
		MOV		[Dest+1+N],AH
		JMP		L3

L1:
		CMP		AL,CH
		JB		L2
		MOV		AL,CH
		
L2:		MOV		[Dest+1+N],AL
L3:

									

ENDM

ENDM


Aver	MACRO	Left, Right, Top, Bottom
		MOVZX	AX,BYTE PTR Left
		MOVZX	CX,BYTE PTR Right
		SHL		EAX,16
		SHL		ECX,16
		MOVZX	AX,BYTE PTR Top
		MOVZX	CX,BYTE PTR Bottom
		LEA		EAX,[EAX+ECX+10001h]
		SHR		EAX,1
		MOV		AH,AL
		SHR		EAX,8
		MOVZX	ECX,AH
		AND		EAX,0FFh
		LEA		EAX,[EAX+ECX+1]
		SHR		EAX,1					
ENDM			



Extrem_Filter8_Aver	MACRO	Dest, Top, Center, Bottom

IRPC	N,<04>
		MOV		EAX,[Top+N]
		MOV		ECX,[Bottom+N]
		MinMax4	JBE
		MOV		[Min8+N],EAX

		MOV		EAX,[Top+N]
		MinMax4	JAE
		MOV		[Max8+N],EAX

		MOV		EAX,[Center+N]
		MOV		ECX,[Center+N+2]
		MinMax4	JBE
		MOV		[Min8Center+N],EAX

		MOV		EAX,[Center+N]
		MinMax4	JAE
		MOV		[Max8Center+N],EAX

ENDM

		MOV		AX,[Top+8]
		MOV		CX,[Bottom+8]
		MinMax2	JBE
		MOV		[Min8+8],AX

		MOV		AX,[Top+8]
		MinMax2	JAE
		MOV		[Max8+8],AX

		


IRPC	N,<01234567>
LOCAL	L1, L2, L3, L4

		MOV		EAX,[Min8+N-1]
		MOV		ECX,[Max8+N-1]
		MOV		AL,[Min8Center+N]
		MOV		CL,[Max8Center+N]

		MinMax	JBE, AH,AL
		MinMax	JAE, CH,CL
		SHR		EAX,8
		SHR		ECX,8
		MinMax	JBE, AH,AL
		MinMax	JAE, CH,CL
		SHR		EAX,8
		SHR		ECX,8
		MinMax	JBE, AH,AL
		MinMax	JAE, CH,CL

		;; AH=Min CH=Max

		MOV		AL,[Center+N+1]

		CMP		AL,AH
		JBE		L1
		CMP		AL,CH
		JB		L4


L1:		MOVZX	EAX,AH
		MOVZX	ECX,CH
		SUB		ECX,EAX
		CMP		ECX,Max_Min
		JGE		L2



		Aver	[Center+N], [Center+N+2], [Top+N+1], [Bottom+N+1]
		JMP		L3			

L2:		Aver	[Center+N], [Center+N+2], [Top+N+1], [Bottom+N+1]
		MOVZX	ECX,BYTE PTR [Center+N+1]		
		LEA		EAX,[EAX+ECX+1]
		SHR		EAX,1
		
		
L3:		MOV		[Dest+1+N],AL
L4:
									

ENDM

ENDM


;====================================================================
;
;				D A T A
;
;====================================================================
				.DATA
				ALIGN	8



;====================================================================
;
;				C O D E
;
;====================================================================
				.CODE

;___________________________________________________________________________________________________________________________
;
;				INTEL_P_Filter__NO_MMX
;___________________________________________________________________________________________________________________________
INTEL_P_Filter__NO_MMX	PROC	C, Buf:DWORD, W:DWORD, H:DWORD, CenterScale:DWORD
		PUSHAD

;________________LOCAL VARIABLES__________________________________________________________
		MOV		EAX,[W]
				Len = 8+4+4+4 
		LEA		EAX,[EAX*2+16*2+Len]
		SUB		ESP,EAX										; WARNING!!!!!!! 
				Save8			EQU QWORD PTR [ESP]			; 
				SaveEDX			EQU DWORD PTR [ESP+8]		; DONT USE PUSH-POP-CALL
				Counter			EQU DWORD PTR [ESP+12]
				SaveEBP			EQU DWORD PTR [ESP+16]	

;_____________________________________________________________________________________
		
		LEA		ESI,[ESP+Len+8]
		AND		ESI,not 7		
		LEA		EDI,[ESI+2+8]
		ADD		EDI,[W]
		AND		EDI,not 7		
		INC		ESI
		INC		EDI			

		CScale = -1
REPT	16
LOCAL	Continue, L5, L10, L20 
		CScale = CScale + 1

		CMP		[CenterScale],CScale
		JNE		Continue


		MOV		EBX,[W]

		DEC		[H]


		MOV		EDX,[Buf]

		CopyLine ESI

L5:		CopyLine EDI
		
		
		LEA		ECX,[EBX+7]
		SHR		ECX,3
		MOV		[Counter],ECX
		MOV		EAX,0

		MOV		[SaveEDX],EDX
		MOV		[SaveEBP],EBP

		MOV		ECX,[EDX+EBX]
		MOV		DWORD PTR [Save8],ECX
		MOV		ECX,[EDX+EBX+4]
		MOV		DWORD PTR [Save8+4],ECX
		
		
L10:
		P_Filter8	EDX, ESI+EAX, EDI+EAX, EDX+EBX, CScale 
						
		ADD		EAX,8
		ADD		EDX,8
		DEC		[Counter]
		JNE		L10

		MOV		EDX,[SaveEDX]
		MOV		EBP,[SaveEBP]

		MOV		ECX,DWORD PTR [Save8]
		MOV		[EDX+EBX],ECX
		MOV		ECX,DWORD PTR [Save8+4]
		MOV		[EDX+EBX+4],ECX

		XCHG	ESI,EDI
		ADD		EDX,EBX
		DEC		[H]
		JNE		L5


		CopyLine EDI
		
		
		LEA		ECX,[EBX+7]
		SHR		ECX,3
		MOV		[Counter],ECX
		MOV		EAX,0
		MOV		[SaveEDX],EDX
		MOV		[SaveEBP],EBP

		MOV		ECX,[EDX+EBX]
		MOV		DWORD PTR [Save8],ECX
		MOV		ECX,[EDX+EBX+4]
		MOV		DWORD PTR [Save8+4],ECX
		
L20:
		P_Filter8	EDX, ESI+EAX, EDI+EAX, EDI+EAX, CScale 
						
		ADD		EAX,8
		ADD		EDX,8
		DEC		[Counter]
		JNE		L20

		MOV		EDX,[SaveEDX]
		MOV		EBP,[SaveEBP]

		MOV		ECX,DWORD PTR [Save8]
		MOV		[EDX+EBX],ECX
		MOV		ECX,DWORD PTR [Save8+4]
		MOV		[EDX+EBX+4],ECX

		JMP		@@100
Continue:
ENDM

@@100:
		
		MOV		EAX,[W]
		LEA		EAX,[EAX*2+16*2+Len]
		ADD		ESP,EAX	
		POPAD

		RET		

INTEL_P_Filter__NO_MMX	ENDP



;___________________________________________________________________________________________________________________________
;
;				INTEL_Extrem_Filter_MinMax__NO_MMX
;___________________________________________________________________________________________________________________________
INTEL_Extrem_Filter_MinMax__NO_MMX	PROC	C, Buf:DWORD, W:DWORD, H:DWORD
		PUSHAD

;________________LOCAL VARIABLES__________________________________________________________
		MOV		EAX,[W]
				Len = 8+4+4+4+16+16+8+8 
		LEA		EAX,[EAX*2+16*2+Len]
		SUB		ESP,EAX										; WARNING!!!!!!! 
				Save8			EQU QWORD PTR [ESP]			; 
				SaveEDX			EQU DWORD PTR [ESP+8]		; DONT USE PUSH-POP-CALL
				Counter			EQU DWORD PTR [ESP+12]
				SaveEBP			EQU DWORD PTR [ESP+16]
				Min8			EQU ESP+20
				Max8			EQU	ESP+20+16	
				Min8Center		EQU ESP+20+32
				Max8Center		EQU	ESP+20+32+8	

;_____________________________________________________________________________________
		
		LEA		ESI,[ESP+Len+8]
		AND		ESI,not 7		
		LEA		EDI,[ESI+2+8]
		ADD		EDI,[W]
		AND		EDI,not 7		



		MOV		EBX,[W]

		DEC		[H]


		MOV		EDX,[Buf]

		CopyLine1 ESI

@@5:	CopyLine1 EDI
		
		
		LEA		ECX,[EBX+7-2]
		SHR		ECX,3
		MOV		[Counter],ECX

		MOV		[SaveEDX],EDX
		MOV		[SaveEBP],EBP

		MOV		ECX,[EDX+EBX-1]
		MOV		DWORD PTR [Save8],ECX
		MOV		ECX,[EDX+EBX+4-1]
		MOV		DWORD PTR [Save8+4],ECX
		
		MOV		EBP,0
		
@@10:
		Extrem_Filter8_MinMax	EDX, ESI+EBP, EDI+EBP, EDX+EBX 
						
		ADD		EBP,8
		ADD		EDX,8
		DEC		[Counter]
		JNE		@@10

		MOV		EDX,[SaveEDX]
		MOV		EBP,[SaveEBP]

		MOV		ECX,DWORD PTR [Save8]
		MOV		[EDX+EBX-1],ECX
		MOV		ECX,DWORD PTR [Save8+4]
		MOV		[EDX+EBX+4-1],ECX

		XCHG	ESI,EDI
		ADD		EDX,EBX
		DEC		[H]
		JNE		@@5


		CopyLine1 EDI
		
		
		LEA		ECX,[EBX+7-2]
		SHR		ECX,3
		MOV		[Counter],ECX

		MOV		[SaveEDX],EDX
		MOV		[SaveEBP],EBP

		MOV		ECX,[EDX+EBX-1]
		MOV		DWORD PTR [Save8],ECX
		MOV		ECX,[EDX+EBX+4-1]
		MOV		DWORD PTR [Save8+4],ECX
		
		MOV		EBP,0

@@20:
		Extrem_Filter8_MinMax	EDX, ESI+EBP, EDI+EBP, EDI+EBP 
						
		ADD		EBP,8
		ADD		EDX,8
		DEC		[Counter]
		JNE		@@20

		MOV		EDX,[SaveEDX]
		MOV		EBP,[SaveEBP]

		MOV		ECX,DWORD PTR [Save8]
		MOV		[EDX+EBX-1],ECX
		MOV		ECX,DWORD PTR [Save8+4]
		MOV		[EDX+EBX+4-1],ECX

		
		MOV		EAX,[W]
		LEA		EAX,[EAX*2+16*2+Len]
		ADD		ESP,EAX	
		POPAD

		RET		

INTEL_Extrem_Filter_MinMax__NO_MMX	ENDP


;___________________________________________________________________________________________________________________________
;
;				INTEL_Extrem_Filter_Aver__NO_MMX
;___________________________________________________________________________________________________________________________
INTEL_Extrem_Filter_Aver__NO_MMX	PROC	C, Buf:DWORD, W:DWORD, H:DWORD, Max_Min0:DWORD
		PUSHAD

;________________LOCAL VARIABLES__________________________________________________________
		MOV		EAX,[W]
				Len = 8+4+4+4+16+16+8+8+4 
		LEA		EAX,[EAX*2+16*2+Len]
		SUB		ESP,EAX										; WARNING!!!!!!! 
				Save8			EQU QWORD PTR [ESP]			; 
				SaveEDX			EQU DWORD PTR [ESP+8]		; DONT USE PUSH-POP-CALL
				Counter			EQU DWORD PTR [ESP+12]
				SaveEBP			EQU DWORD PTR [ESP+16]
				Min8			EQU ESP+20
				Max8			EQU	ESP+20+16	
				Min8Center		EQU ESP+20+32
				Max8Center		EQU	ESP+20+32+8	
				Max_Min			EQU	DWORD PTR [ESP+20+32+8+8]


;_____________________________________________________________________________________
		
		LEA		ESI,[ESP+Len+8]
		AND		ESI,not 7		
		LEA		EDI,[ESI+2+8]
		ADD		EDI,[W]
		AND		EDI,not 7		



		MOV		EBX,[W]

		DEC		[H]

		MOV		EAX,[Max_Min0]
		MOV		Max_Min,EAX


		MOV		EDX,[Buf]

		CopyLine1 ESI

@@5:	CopyLine1 EDI
		
		
		LEA		ECX,[EBX+7-2]
		SHR		ECX,3
		MOV		[Counter],ECX

		MOV		[SaveEDX],EDX
		MOV		[SaveEBP],EBP

		MOV		ECX,[EDX+EBX-1]
		MOV		DWORD PTR [Save8],ECX
		MOV		ECX,[EDX+EBX+4-1]
		MOV		DWORD PTR [Save8+4],ECX
		
		MOV		EBP,0
		
@@10:
		Extrem_Filter8_Aver	EDX, ESI+EBP, EDI+EBP, EDX+EBX 
						
		ADD		EBP,8
		ADD		EDX,8
		DEC		[Counter]
		JNE		@@10

		MOV		EDX,[SaveEDX]
		MOV		EBP,[SaveEBP]

		MOV		ECX,DWORD PTR [Save8]
		MOV		[EDX+EBX-1],ECX
		MOV		ECX,DWORD PTR [Save8+4]
		MOV		[EDX+EBX+4-1],ECX

		XCHG	ESI,EDI
		ADD		EDX,EBX
		DEC		[H]
		JNE		@@5


		CopyLine1 EDI
		
		
		LEA		ECX,[EBX+7-2]
		SHR		ECX,3
		MOV		[Counter],ECX

		MOV		[SaveEDX],EDX
		MOV		[SaveEBP],EBP

		MOV		ECX,[EDX+EBX-1]
		MOV		DWORD PTR [Save8],ECX
		MOV		ECX,[EDX+EBX+4-1]
		MOV		DWORD PTR [Save8+4],ECX
		
		MOV		EBP,0

@@20:
		Extrem_Filter8_Aver	EDX, ESI+EBP, EDI+EBP, EDI+EBP 
						
		ADD		EBP,8
		ADD		EDX,8
		DEC		[Counter]
		JNE		@@20

		MOV		EDX,[SaveEDX]
		MOV		EBP,[SaveEBP]

		MOV		ECX,DWORD PTR [Save8]
		MOV		[EDX+EBX-1],ECX
		MOV		ECX,DWORD PTR [Save8+4]
		MOV		[EDX+EBX+4-1],ECX

		
		MOV		EAX,[W]
		LEA		EAX,[EAX*2+16*2+Len]
		ADD		ESP,EAX	
		POPAD

		RET		

INTEL_Extrem_Filter_Aver__NO_MMX	ENDP



END
